package com.site.user.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.site.data.QueryResult;
import com.site.user.pojo.TbOperation;
import com.site.user.service.ITbOperationService;

@Controller
@RequestMapping("/operation")
public class TbOperationController extends BaseController {
	@Resource
	private ITbOperationService tbOperationService;

	/**
	 * 查询数据列表
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_save", method = RequestMethod.POST)
	public void save(HttpServletRequest request, HttpServletResponse response, PrintWriter out, TbOperation entity) {
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		String id = UUID.randomUUID().toString().replace("-", "");
		entity.setIdOperation(id);
		tbOperationService.execSave(entity);

		String msg = "保存成功.";
		out.println(msg);
	}

	/**
	 * 查询数据
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_search", method = RequestMethod.POST)
	public void search(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");

		String whereJPQL = "1=1";

		Map mpparam = new HashMap();

		String opetKey = ServletRequestUtils.getStringParameter(request, "opetKey", "");
		if (StringUtils.isNotEmpty(opetKey)) {
			whereJPQL += " and opetKey like :opetKey";
			mpparam.put("opetKey", "%" + opetKey + "%");
		}

		String opetName = ServletRequestUtils.getStringParameter(request, "opetName", "");
		if (StringUtils.isNotEmpty(opetName)) {
			whereJPQL += " and opetName like :opetName";
			mpparam.put("opetName", "%" + opetName + "%");
		}

		
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("opetKey", "DESC");

		int pagesize = ServletRequestUtils.getIntParameter(request, "pagesize", 10);
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		int maxResult = pagesize * page;
		int startIndex = pagesize * (page - 1);

		QueryResult result = tbOperationService.queryData(startIndex, maxResult, whereJPQL, mpparam, orderby);

		out.println(result.getResult("json"));

	}

	/**
	 * 载入数据
	 * @param request
	 * @param response
	 * @param out
	 */

	@RequestMapping(value = "/ser_load", method = RequestMethod.GET)
	public void load(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");

		String id = ServletRequestUtils.getStringParameter(request, "id", "");
		TbOperation obj = null;

		if (StringUtils.isNotEmpty(id)) {

			obj = tbOperationService.execFindById(id);
			if (obj != null) {
				JSONObject jobj = new JSONObject(obj);
				out.println(jobj.toString());
			}
		} else {
			out.println("");
		}

	}

	/**
	 * 删除数据
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_delete", method = RequestMethod.POST)
	public void delete(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		String msg = "";
		try {
			String ids = ServletRequestUtils.getStringParameter(request, "ids", "");
			if (StringUtils.isNotEmpty(ids)) {
				String wheresql = " o.idOperation in ('" + ids + "')";
				tbOperationService.queryDeleteBatch(wheresql);
				msg = "删除成功!";
			}
		} catch (Exception e) {
			msg = "删除数据出现异常!" + e.getMessage();
		}

		out.println(msg);
	}

	/**
	 * 修改数据
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_update", method = RequestMethod.POST)
	public void update(HttpServletRequest request, HttpServletResponse response, PrintWriter out, TbOperation entity) {
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");

		String msg = "";
		try {
			tbOperationService.execUpdate(entity);
			msg = "记录更新成功！";
		} catch (Exception ex) {
			msg = "系统异常，请稍后再试！"+ex.getMessage();
		}
		out.println(msg);

	}
}
